class union_find{
    private Map<Integer,Integer> parent;
    private Map<Integer,Integer> count;
    public union_find(int[] nums)
    {
        parent = new HashMap<>();
        count = new HashMap<>();
        for(int num:nums)
        {
            parent.put(num,num);
            count.put(num,1);
        }
    }
    public Integer find(int x)
    {
        if(!parent.containsKey(x))
        {
            return null;
        }
        if(parent.get(x)!=x)
        {
            parent.put(x,find(parent.get(x)));
        }
        return parent.get(x);
    }
    public int pair(int x,int y)
    {
        int rootx = find(x);
        int rooty = find(y);
        if(rootx==rooty)
        {
            return count.get(rootx);
        }
        parent.put(rootx,rooty);
        count.put(rooty,count.get(rootx)+count.get(rooty));
        return count.get(rooty);
    }
}

class Solution {
    public int longestConsecutive(int[] nums) {
       if(nums==null||nums.length==0)
       {
        return 0;
       }
       union_find uf = new union_find(nums);
       int ans = 1;
       for(int num:nums)
       {
         if(uf.find(num+1)!=null){
             ans = Math.max(ans,uf.pair(num,num+1));
         }
       }
       return ans;
    }
}